﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Modules.Core;
using Modules.Library.Entities;
using System.Collections;
namespace ComputerStoreManager {
    public static class Model {

        public static string Error;
        public static bool InsertNewData(object obj, KindOfObject kind) {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                if (!con.InsertNewData(obj, kind)) {
                    Error = con.Error;
                    con.Disconnect();
                    return false;
                }
                con.Disconnect();
                return true;
            }
            Error = con.Error;
            return false;
        }

        public static bool UpdateData(object obj, KindOfObject kind) {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                if (!con.UpdateData(obj, kind)) {
                    Error = con.Error;
                    con.Disconnect();
                    return false;
                }
                con.Disconnect();
                return true;
            }
            Error = con.Error;
            return false;
        }

        public static string CheckLogin(string userName, string pass) {
            Connection con = Connection.GetInstance();
            if(con.Connect()) {
                var dt = con.GetDataTable("select * from nhanvien where UserName='" + userName + "' and PassWord='" + pass + "'");
                if(dt!=null && dt.Rows.Count>0) {
                    con.Disconnect();
                    return dt.Rows[0]["MaNV"].ToString();

                }
                con.Disconnect();
                return null;
            }
            Error = con.Error;
            return null;
        }

        public static DataTable SearchInfo(string value, SearchingCondition condition, KindOfObject kind) {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                try {
                    DataTable data = null;
                    switch (kind) {
                        case KindOfObject.BY_CUSTOMER:
                            switch (condition) {
                                case SearchingCondition.BY_NAME:
                                    data = con.GetDataTable("select * from khachhang where HoVaTen like '%" + value + "%'");
                                    break;
                                case SearchingCondition.BY_ID:
                                    data = con.GetDataTable("select * from khachhang where MaKH='" + value + "'");
                                    break;
                            }
                            break;
                        case KindOfObject.BY_EMPLOYEE:
                            switch (condition) {
                                case SearchingCondition.BY_NAME:
                                    data = con.GetDataTable("select * from nhanvien where HoVaTen like '%" + value + "%'");
                                    break;
                                case SearchingCondition.BY_ID:
                                    data = con.GetDataTable("select * from nhanvien where MaNV='" + value+"'");
                                    break;
                            }
                            break;
                        case KindOfObject.BY_PRODUCT:
                            switch(condition) {
                                case SearchingCondition.BY_NAME:
                                    data = con.GetDataTable("select * from linhkien where TenMH like '%" + value + "%'");
                                    break;
                                case SearchingCondition.BY_ID:
                                    data = con.GetDataTable("select * from linhkien where MaMH='" + value + "'");
                                    break;
                            }
                            break;
                        case KindOfObject.BY_INVOICE:
                            data = con.GetDataTable("select * from invoiceresultview");
                            break;
                    }
                    con.Disconnect();
                    return data;
                }
                catch (Exception) {
                    con.Disconnect();
                    return null;
                }
            }
            //con.Disconnect();
            return null;
        }
        public static string GetThongSoPhu(KindOfObject kind) {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                try {
                    DataTable data = con.GetDataTable("select * from thongsophu"); ;
                    switch (kind) {
                        case KindOfObject.BY_CUSTOMER:
                            con.Disconnect();
                            return data.Rows[0]["MaKhachHang"].ToString();
                        case KindOfObject.BY_EMPLOYEE:
                            con.Disconnect();
                            return data.Rows[0]["MaNhanVien"].ToString();
                        case KindOfObject.BY_PRODUCT:
                            con.Disconnect();
                            return data.Rows[0]["MaLinhKien"].ToString();
                        case KindOfObject.BY_INVOICE:
                            con.Disconnect();
                            return data.Rows[0]["MaHoaDon"].ToString();
                    }
                }
                catch (Exception) {
                    con.Disconnect();
                    return null;
                }
            }
            //con.Disconnect();
            return null;
        }

        public static DataTable GetInitInfo(KindOfObject kind) {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                try {
                    switch (kind) {
                        case KindOfObject.BY_CUSTOMER:
                            con.Disconnect();
                            return con.GetDataTable("select * from khachhang"); 
                        case KindOfObject.BY_EMPLOYEE:
                            con.Disconnect();
                            return con.GetDataTable("select * from loainhanvien"); 
                        case KindOfObject.BY_PRODUCT:
                            con.Disconnect();
                            return con.GetDataTable("select * from loailinhkien"); 
                    }
                }
                catch (Exception) {
                    con.Disconnect();
                    return null;
                }
            }
            //con.Disconnect();
            return null;
        }

        public static DataTable GetListNhanVien() {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                var rs = con.GetDataTable("select MaNV, HoVaTen, NgaySinh, GioiTinh, CMND, SoDienThoai, DiaChi, Email, MaLoaiNV, UserName from nhanvien");
                con.Disconnect();
                return rs;
            }
            return null;
        }
        public static DataTable GetListKhachHang() {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                var rs = con.GetDataTable("select * from khachhang");
                con.Disconnect();
                return rs;
            }
            return null;
        }

        public static DataTable GetListLinhKien() {
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                var rs = con.GetDataTable("select * from linhkien");
                con.Disconnect();
                return rs;
            }
            return null;
        }

        public static Hashtable GetLoaiNhanVienInfo() {
            Hashtable result = new Hashtable();
            Connection con = Connection.GetInstance();
            if (con.Connect()) {
                var rs = con.GetDataTable("select * from loainhanvien");
                foreach(DataRow row in rs.Rows) {
                    result.Add(row["MaLoaiNV"].ToString(), row["TenLoaiNV"].ToString());
                }
                con.Disconnect();
            }
            return result;
        }

        public static Hashtable GetLoaiMatHang() {
            Hashtable result = new Hashtable();
            Connection con = Connection.GetInstance();
            if(con.Connect()) {
                var rs = con.GetDataTable("select * from loailinhkien");
                foreach(DataRow row in rs.Rows) {
                    result.Add(row["MaLoaiMH"].ToString(), row["TenLoaiMH"].ToString());
                }
                con.Disconnect();
            }
            return result;
        }

        public static DataTable GetListHoaDon()
        {
            Connection con = Connection.GetInstance();
            if (con.Connect())
            {
                var rs = con.GetDataTable("select * from invoiceresultview");
                con.Disconnect();
                return rs;
            }
            return null;
        }
    }
}
